package zdream.control.test.world;

import org.junit.Test;
import zdream.control.input.SimpleWorldInput;
import zdream.control.input.WorldInput;
import zdream.control.level.LevelManagerForTest;
import zdream.control.profile.mm.MegaManProfile;
import zdream.control.region.Region;
import zdream.control.region.SimpleRegionBuilder;
import zdream.control.world.LevelWorld;

import static org.junit.Assert.*;

/**
 * @author Zdream
 * @date 2023-12-28
 * @since 0.0.1
 */
public class RoomTransmissionCase {
	@Test
	public void testSimpleRegionBuilder1() {
		Region region = new SimpleRegionBuilder(this.getClass().getSimpleName())
				.withRoomPositions("13")
				.withRoom(1)
						.terrain("#########################")
						.terrain("#########################")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                       ")
						.terrain("##                       ")
						.terrain("##          S            ")
						.terrain("#########################")
						.terrain("#########################")
						.terrain("#########################")
						.build()
				.withRoom(3)
						.terrain("#########################")
						.terrain("#########################")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("##                     ##")
						.terrain("                       ##")
						.terrain("                       ##")
						.terrain("                       ##")
						.terrain("#########################")
						.terrain("#########################")
						.terrain("#########################")
						.build()
				.withStartPoint('S')
				.build();

		MegaManProfile p1 = new MegaManProfile();
		SimpleWorldInput input1 = new SimpleWorldInput();
		p1.setInput(input1);
		LevelManagerForTest manager = LevelManagerForTest.builder()
				.withPlayer1(p1)
				.build();
		manager.applyWorld(region);
		manager.runUntil(ignored -> manager.state == LevelWorld.STATE_PLAYING);
		assertNotNull(p1.foe);
		assertNotNull(manager.focusFoe);

		assertEquals(1, manager.currentRoom.id);
		input1.setDown(WorldInput.MAP_RIGHT);
		manager.runUntil(ignored -> p1.foe.anchorX > 20);
		manager.everyTick(ignored -> System.out.printf("%d, %s, trans=%s%n",
				manager.worldTimeInMillis(), p1.foe, manager.inRoomTransmission));
		manager.runUntil(ignored -> p1.foe.pos.right() >= manager.currentRoom.width);

		assertTrue(manager.inRoomTransmission);
		assertEquals(0, manager.roomTransmission.cameraPoses[0].x, 0);
		assertEquals(0, manager.roomTransmission.cameraPoses[0].y, 0);
		assertEquals(0, manager.roomTransmission.cameraPoses[1].x, 0);
		assertEquals(0, manager.roomTransmission.cameraPoses[1].y, 0);
		assertEquals(0, manager.roomTransmission.cameraPoses[2].x, 25);
		assertEquals(0, manager.roomTransmission.cameraPoses[2].y, 0);
		assertEquals(0, manager.roomTransmission.cameraPoses[3].x, 25);
		assertEquals(0, manager.roomTransmission.cameraPoses[3].y, 0);

		manager.clearTicks();
		manager.everyTick(ignored -> System.out.printf("%d, %s, trans.phase=%s%n",
				manager.worldTimeInMillis(), p1.foe, manager.roomTransmission.phase));
		manager.runUntil(ignored -> !manager.inRoomTransmission);

		assertEquals(3, manager.currentRoom.id);
	}
}
